{% extends 'basic.html' %}
{% load static %}
{% block title %}
    短信登录
{% endblock %}


{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: red;
            position: absolute;
            font-size: 13px;
        }

        #btnSms:hover {
            background-color: lightgray;
        }

        .account {
            margin-top: 20vh;
            border-radius: 15px;
            box-shadow: 15px 15px 15px 10px lightgray;
        }

        .login_pwd {
            font-style: italic;
        }

        .login_pwd:hover {
            text-decoration: none;
        }
    </style>
{% endblock %}


{% block content %}
    <div class="account">
        <div class="title">短信登录</div>
        <form id="smsForm" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row ">
                            <div class="col-xs-7" style="margin-left: 13px; width: 300px">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5" style="margin-left: 80px;">
                                <input id="btnSms" type="button" class="btn btn-default" value="点击获取验证码"
                                       style="border-radius: 15px;border: 1px lightgray solid;">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}

            <div class="row">
                <div class="col-xs-3" style="margin-left: 13px">
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="登  录"/>
                </div>
                <div class="col-xs-3" style="margin-left: 320px">
                    <a href="{% url 'login_pwd' %}" class="login_pwd">用户名和密码登录</a>
                </div>
            </div>
        </form>
    </div>
{% endblock %}


{% block js %}
    <script>
        //页面加载完成自动执行
        $(function () {
            BindClickSms();
            loginClick();
        })

        /*
        * 点击获取验证码，用于用户登录
        * */
        function BindClickSms() {
            $('#btnSms').click(function () {
                $('.error-msg').empty()
                //拿到用户输入的邮箱
                var email = $('#id_email').val()
                //发送ajax请求
                $.ajax({
                    url: '{% url 'login_sms' %}',
                    type: 'GET',
                    data: {'email': email},
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            sendSmsRemind();
                        } else {
                            //错误信息
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0])
                            })
                        }
                    }
                })
            })
        }

        /*
       * 验证码倒计时
       * */
        function sendSmsRemind() {
            var $btnSms = $('#btnSms');
            $btnSms.prop('disabled', true);   //禁用点击
            var time = 60
            clearInterval(remind); // 先清除可能存在的旧定时器
            var remind = setInterval(() => {
                $btnSms.val(time + '秒重新发送');
                time = time - 1;
                if (time < 1) {
                    clearInterval(remind);
                    $btnSms.val('点击获取验证码').prop('disabled', false);
                }
            }, 1000)
        }

        /**
         * 点击登录
         * */
        function loginClick() {
            $('#btnSubmit').click(function () {
                $('.error-msg').empty()  //清楚错误信息
                //向后端发送ajax请求
                $.ajax({
                    url: '{% url 'login' %}',
                    type: 'POST',
                    data: $('#smsForm').serialize(),   //form表单里的使用输入框，加上crsf_token
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            console.log(res)
                            location.href = res.data
                        } else {
                            //错误信息
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0])
                            })
                        }
                    }
                })
            })
        }
    </script>
{% endblock %}